package com.shiwaixiangcun.monitor.service.impl;

import com.shiwaixiangcun.core.service.BaseServiceImpl;
import com.shiwaixiangcun.monitor.domain.WorkOrder;
import com.shiwaixiangcun.monitor.domain.WorkOrderServiceRecord;
import com.shiwaixiangcun.monitor.dto.WorkOrderServiceRecordDto;
import com.shiwaixiangcun.monitor.enums.WorkOrderServiceType;
import com.shiwaixiangcun.monitor.repository.WorkOrderServiceRecordRepository;
import com.shiwaixiangcun.monitor.security.SecurityUtils;
import com.shiwaixiangcun.monitor.service.WorkOrderServiceRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/23.
 */
@Service
@Transactional
public class WorkOrderServiceRecordServiceImpl extends BaseServiceImpl<WorkOrderServiceRecord, Long> implements WorkOrderServiceRecordService {

    @Autowired
    private WorkOrderServiceRecordRepository workOrderServiceRecordRepository;

    @Override
    public WorkOrderServiceRecord createServiceRecord(WorkOrder workOrder, WorkOrderServiceType type, String content) {
        WorkOrderServiceRecord record = new WorkOrderServiceRecord();
        record.setWorkOrder(workOrder);
        record.setWorkOrderServiceType(type);
        record.setContent(content);
        record.setUser(SecurityUtils.getCurrentUser());
        return workOrderServiceRecordRepository.save(record);
    }

    @Override
    public List<WorkOrderServiceRecordDto> getServiceRecordList(WorkOrder workOrder) {
        List<WorkOrderServiceRecordDto> dtoList = new ArrayList<>();
        List<WorkOrderServiceRecord> list = workOrderServiceRecordRepository.getServiceList(workOrder);
        WorkOrderServiceRecordDto dto;
        for (WorkOrderServiceRecord record : list) {
            dto = new WorkOrderServiceRecordDto();
            dto.setContent(record.getContent());
            dto.setName(record.getUser().getName());
            dto.setTime(record.getCreateTime());
            dto.setType(record.getWorkOrderServiceType());
            dtoList.add(dto);
        }
        return dtoList;
    }
}
